package com.robot.backend.controller.photo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.robot.backend.utils.photo.QiniuUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/upload/")
public class photoUpload {
    @Autowired
    QiniuUtils qiniuUtils;

    @PostMapping("")
    String photoUpload(@RequestParam("image") MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        //唯一的文件名称 使用UUID
        String oldName = file.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
        System.out.println(file);
        //上传文件到七牛云
        boolean upload = qiniuUtils.upload(file, newName);
        Map<String,String> map = new HashMap<>();
        if(upload){
            System.out.println(QiniuUtils.url  +newName);
            map.put("location",QiniuUtils.url+newName);
            return QiniuUtils.url  +newName;
        }
        map.put("error","error");
        return QiniuUtils.url  +newName;
    }
}
